package cn.tiancaifan.micro.controller.system.basic;

import cn.tiancaifan.micro.entity.Position;
import cn.tiancaifan.micro.entity.SystemResults;
import cn.tiancaifan.micro.service.PositionService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/basic/pos")
public class PositionController {

    @Autowired
    PositionService positionService;

    @GetMapping("/")
    public SystemResults getAllPositions(){
        SystemResults ok = SystemResults.ok(null, positionService.getAllPositons());
        return ok;
    }

    @PostMapping("/")
    public SystemResults addPosition(@RequestBody Position position){
        if (positionService.addPosition(position) == 1){
            return SystemResults.ok("添加成功");
        }else {
            return SystemResults.error("添加失败");
        }
    }

    @PutMapping("/")
    public SystemResults updataPosition(@RequestBody Position position){
        if (positionService.updataPosition(position) == 1){
            return SystemResults.ok("更新成功");
        }else {
            return SystemResults.error("更新失败");
        }
    }

    @DeleteMapping("/{id}")
    public SystemResults deletePositionById(@PathVariable Integer id){
        if (positionService.deletePositionById(id) == 1){
            return SystemResults.ok("删除成功");
        }else {
            return SystemResults.ok("删除失败");
        }
    }

    @DeleteMapping("/")
    public SystemResults deletePositionsByIds(@Param("ids") Integer[] ids){
        if (positionService.deletePositionsByIds(ids) == ids.length){
            return SystemResults.ok("删除成功");
        }else {
            return SystemResults.ok("删除失败");
        }
    }
}
